#include "myfirst.h"
#include "triangle.h"
#include "square.h"
#include "circle.h"
#include <QVector>
#include <QList>
#include <QDebug>
#include <QFile>
#include <QDir>
#include <QTextStream>
#include <QString>
#include <QList>
#include <cmath>

Conteiner::~Conteiner()
{
    for(int i=0; i<mass.size();i++)
    {
        delete (mass.at(i));
    }
}

int Conteiner::size()
{
    return mass.size();
}

void Conteiner::pushFig(Figure *a)
{
     mass.push_back(a);
}
void Conteiner::removeFig(int i)
{
    mass.remove(i);
}

void Conteiner::zapisb(QString name_txt)
{
    QFile file(name_txt);
       if (!file.open(QFile::WriteOnly | QFile::Text))
       {
          return;
       }
                QTextStream stream(&file);

                for (int i = 0; i < mass.size(); i++)
                {
                    stream << mass[i]->print_name() << endl;
                    QList<QString> inf = mass[i]->print_inf();
                    for(int j = 0; j<inf.size(); j++)
                    {
                        stream<<inf[j]<<endl;
                    }
                }
                file.close();
}

void Conteiner::readFromFile(QString name_txt)
{
    QFile file(name_txt);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        qDebug() <<"erorr";
           return;
    }
    QTextStream in(&file);
             while (!in.atEnd())
             {
                 QString name = in.readLine();
                 if (name == "Square")
                 {
                     float x = in.readLine().toFloat();
                     float x1=in.readLine().toFloat();
                     float x2=in.readLine().toFloat();
                     float turn=in.readLine().toFloat();
                     Square *fig = new Square(x,x1,x2,turn);
                     pushFig(fig);
                 }
                 else if (name == "Circle")
                 {
                     float x = in.readLine().toFloat();
                     float x1=in.readLine().toFloat();
                     float x2=in.readLine().toFloat();
                     float turn=in.readLine().toFloat();
                     Circle *fig = new Circle(x,x1,x2,turn);
                     pushFig(fig);
                 }
                 else if (name == "Triangle")
                 {
                     float x = in.readLine().toFloat();
                     float x1=in.readLine().toFloat();
                     float x2=in.readLine().toFloat();
                     float turn=in.readLine().toFloat();
                     Triangle *fig = new Triangle(x,x1,x2,turn);
                     pushFig(fig);
                 }
             }
             file.close();
}

void Conteiner::ChengeXY(int i, float firstX, float secondX)
{
    mass[i]->chengeKoordinat(firstX,secondX);
}

Figure *Conteiner::operator[](int i)
{
    return mass[i];
}
